package com.andyou.modules.content.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.andyou.common.utils.GridVO;
import com.andyou.common.config.Global;
import com.andyou.common.mapper.JsonMapper;
import com.andyou.common.persistence.Page;
import com.andyou.common.web.BaseController;
import com.andyou.common.utils.StringUtils;
import com.andyou.common.utils.excel.ExportExcel;
import com.andyou.common.utils.DateUtils;
import com.google.common.collect.Lists;
import com.andyou.modules.content.entity.ContentPublish;
import com.andyou.modules.content.service.ContentPublishService;

/**
 * 内容发布管理（content_publish）Controller
 * @author lgx
 * @version 2020-03-08
 */
@Controller
@RequestMapping(value = "${adminPath}/content/contentPublish")
public class ContentPublishController extends BaseController {

	@Autowired
	private ContentPublishService contentPublishService;
	
	@ModelAttribute
	public ContentPublish get(@RequestParam(required=false) String id) {
		ContentPublish entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = contentPublishService.get(id);
		}
		if (entity == null){
			entity = new ContentPublish();
		}
		return entity;
	}
	
	@RequiresPermissions("content:contentPublish:statList")
	@RequestMapping(value = {"statList"})
	public String statList(ContentPublish contentPublish, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/content/statList";
	}
	
	@RequiresPermissions("content:contentPublish:stat_dyList")
	@RequestMapping(value = {"stat_dyList"})
	public String stat_dyList(ContentPublish contentPublish, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/content/stat_dyList";
	}
	
	@RequiresPermissions("content:contentPublish:stat_ksList")
	@RequestMapping(value = {"stat_ksList"})
	public String stat_ksList(ContentPublish contentPublish, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/content/stat_ksList";
	}
	
	@RequiresPermissions("content:contentPublish:stat_xhsList")
	@RequestMapping(value = {"stat_xhsList"})
	public String stat_xhsList(ContentPublish contentPublish, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/content/stat_xhsList";
	}
	
	@RequiresPermissions("content:contentPublish:view")
	@RequestMapping(value = {"contentPublishList", ""})
	public String list(ContentPublish contentPublish, HttpServletRequest request, HttpServletResponse response, Model model) {
		//Page<ContentPublish> page = contentPublishService.findPage(new Page<ContentPublish>(request, response), contentPublish); 
		//model.addAttribute("page", page);
		return "modules/content/contentPublishList";
	}
	
	@RequiresPermissions("content:contentPublish:view")
	@ResponseBody
	@RequestMapping(value = { "contentPublishListJson" })
	public String contentPublishListJson(ContentPublish contentPublish, HttpServletRequest request,
			HttpServletResponse response, Model model) {
	
		Page<ContentPublish> page = contentPublishService.findPage(new Page<ContentPublish>(request, response,true),
				contentPublish);
		model.addAttribute("page", page);

		GridVO gridVO = new GridVO();
		gridVO.setRows(page.getList());
		gridVO.setTotal(page.getCount());
		
		return JsonMapper.getInstance().toJson(gridVO);
	}

	@RequiresPermissions("content:contentPublish:view")
	@RequestMapping(value = "contentPublishForm")
	public String form(ContentPublish contentPublish, Model model) {
		model.addAttribute("contentPublish", contentPublish);
		return "modules/content/contentPublishForm";
	}
	
	@RequiresPermissions("content:contentPublish:view")
	@RequestMapping(value = "contentPublishDetail")
	public String contentPublishDetail(ContentPublish contentPublish, Model model){
		model.addAttribute("contentPublish", contentPublish);
		return "modules/content/contentPublishDetail";
	}

	@RequiresPermissions(value={"content:contentPublish:edit","content:contentPublish:create"},logical = Logical.OR)
	@RequestMapping(value = "save")
	public String save(ContentPublish contentPublish, Model model, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, contentPublish)){
			return form(contentPublish, model);
		}
		contentPublishService.save(contentPublish);
		//renderStringByJbox(response, "提交成功");
		//return null;
		return "redirect:"+Global.getAdminPath()+"/content/contentPublish/?repage";
		
	}
	
	@RequiresPermissions("content:contentPublish:delete")
	@RequestMapping(value = "delete")
	public String delete(ContentPublish contentPublish, RedirectAttributes redirectAttributes) {
		contentPublishService.delete(contentPublish);
		addMessage(redirectAttributes, "删除内容发布管理成功");
		return "redirect:"+Global.getAdminPath()+"/content/contentPublish/?repage";
	}
	
	@RequiresPermissions("content:contentPublish:delete")
    @ResponseBody
	@RequestMapping(value = "deleteAjax")
	public String deleteAjax(ContentPublish contentPublish, RedirectAttributes redirectAttributes) {
		contentPublishService.delete(contentPublish);
		return "删除成功";
	}
	
	@RequiresPermissions("content:contentPublish:delete")
	@ResponseBody
	@RequestMapping(value = "batchDelete") 
	public String batchDelete(String ids, RedirectAttributes redirectAttributes) {
		
        String[]  idsArr =  ids.split(",");
        List<String> idList = Lists.newArrayList();
        idList = Arrays.asList(idsArr);
        
        contentPublishService.batchDelete(idList);
	
		return "删除成功";
	}
	
	@RequestMapping(value = "export")
	public String exportFile(ContentPublish contentPublish, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "内容发布管理" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Page<ContentPublish> page_ = new Page<ContentPublish>(request, response, -1);
			page_.setPageSize(500000);
			contentPublish.setPage(page_);
			page_ = contentPublishService.findPage(page_, contentPublish);
			new ExportExcel("内容发布管理", ContentPublish.class).setDataList(page_.getList())
					.write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出数据失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath() + "/content/contentPublish/?repage";
	}

}